package org.jiuling.aliyun.oss.operation;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.jiuling.aliyun.oss.client.factory.AliYunOssClientFactory;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 对象操作
 * @author 紫灵
 * @date 2024年06月21日 20时56分55秒
 */
public class AliYunObjectOperation {

    /** 阿里云对象存储客户端工厂 */
    private final AliYunOssClientFactory aliYunOssClientFactory;

    public AliYunObjectOperation(AliYunOssClientFactory aliYunOssClientFactory) {
        this.aliYunOssClientFactory = aliYunOssClientFactory;
    }


    /**
     * 通过InputStream上传对象
     * @param bucketName 存储空间名称
     * @param key 对象名称
     * @param inputStream 上传的文件流
     * @return 上传结果
     */
    public PutObjectResult uploudByInputStream(String bucketName, String key, InputStream inputStream){
        OSS ossClient = aliYunOssClientFactory.buildOssClient();
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream);
        try {
            // 创建PutObject请求。
            return ossClient.putObject(putObjectRequest);
        }finally {
            aliYunOssClientFactory.closeClient(ossClient);
        }
    }

    /**
     * 通过文件上传
     * @param bucketName 存储空间名称
     * @param key 对象名称
     * @param file 文件
     * @return 上传结果
     */
    public PutObjectResult uploudByFile(String bucketName, String key, File file){
        OSS ossClient = aliYunOssClientFactory.buildOssClient();
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        try {
            // 创建PutObject请求。
            return ossClient.putObject(putObjectRequest);
        }finally {
            aliYunOssClientFactory.closeClient(ossClient);
        }
    }

    /**
     * 获取对象列表
     * @param bucketName 存储空间名称
     * @param keyPrefix key前缀
     * @return 对象摘要信息列表
     */
    public List<OSSObjectSummary> getObjectList(String bucketName, String keyPrefix){
        OSS ossClient = aliYunOssClientFactory.buildOssClient();
        try {
            return ossClient.listObjects(bucketName,keyPrefix).getObjectSummaries();
        }finally {
            aliYunOssClientFactory.closeClient(ossClient);
        }
    }

    /**
     * 获取对象列表
     * @param bucketName 存储空间名称
     * @return 对象摘要信息列表
     */
    public List<OSSObjectSummary> getObjectList(String bucketName){
        return getObjectList(bucketName,null);
    }

    /**
     * 获取对象
     * @param bucketName 存储空间名称
     * @param key 对象名称
     * @return 存储对象
     */
    public OSSObject getObject(String bucketName, String key){
        OSS ossClient = aliYunOssClientFactory.buildOssClient();
        try {
            return ossClient.getObject(bucketName,key);
        }finally {
            aliYunOssClientFactory.closeClient(ossClient);
        }
    }

    /**
     * 获取对象URL
     * @param bucketName 存储空间名称
     * @param key 对象名称
     * @return 对象url
     */
    public String getObjectUrl(String bucketName, String key){
        OSS ossClient = aliYunOssClientFactory.buildOssClient();
        try {
            // 指定生成的签名URL过期时间，单位为毫秒。本示例以设置过期时间为1小时为例。
            Date expiration = new Date(new Date().getTime() + 3600 * 1000L);
            return ossClient.generatePresignedUrl(bucketName,key,expiration).toString();
        }finally {
            aliYunOssClientFactory.closeClient(ossClient);
        }
    }

    /**
     * 删除对象
     * @param bucketName 存储空间名称
     * @param key 对象名称
     */
    public void deleteObject(String bucketName,String key){
        OSS ossClient = aliYunOssClientFactory.buildOssClient();
        try {
            ossClient.deleteObject(bucketName,key);
        }finally {
            aliYunOssClientFactory.closeClient(ossClient);
        }
    }

}
